package com.yun.zf.app.controller;

import com.yun.zf.common.vo.Response;
import com.yun.zf.system.security.utils.SecurityUtils;
import com.yun.zf.system.service.SysCouponReceiveService;
import com.yun.zf.system.service.SysCouponService;
import com.yun.zf.system.service.ZfInviteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;


@RestController
@Api(value = "邀请分享")
@RequestMapping("/share")
public class ShareController {
    @Autowired
    private ZfInviteService inviteService;
    @Autowired
    private SysCouponService couponService;
    @Autowired
    private SysCouponReceiveService receiveService;

    @PostMapping("/invite")
    @ApiOperation(value = "邀请分享功能")
    public Response invite(String title, String cityCode) {
            // todo 生成邀请链接或者海报分享给用户

        return Response.success();
    }


    @PostMapping("/inviteHistory")
    @ApiOperation(value = "我的邀请统计")
    public Response inviteHistory() {
        // 查看邀请总人数，并计算可领取金额
      Long userId=  SecurityUtils.getLoginUser().getUserId();
      // 我的邀请人数
       int total = inviteService.getMyinvite(userId);
       // 可领取金额
        int coupon = total*50;
        Map<String,Integer> map = new HashMap<>();
        map.put("total",total);
        map.put("coupon",coupon);
        return Response.success(map);
    }

    @PostMapping("/inviteCouponList")
    @ApiOperation(value = "根据活动获取优惠券列表")
    public Response inviteCouponList(@RequestBody Map<String,String> map) {
        //查询邀请券列表
        return Response.success(couponService.getCouponList(map.get("activityId")));
    }

    @PostMapping("/inviteCoupon")
    @ApiOperation(value = "领取邀请券")
    public Response inviteCoupon(@RequestBody Map<String,String> map) {
      int c =   receiveService.receiveCoupon(map.get("couponId"),map.get("userId"));
        if (c==-1){
            return Response.error("优惠券已失效或者不存在");
        }
        return Response.success("领取成功");
    }

}
